package com.vx.controller;

import java.sql.Date;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.task.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.activitiFlow.service.IRepositoryBiz;
import com.activitiFlow.service.ITaskBiz;
import com.activitiFlow.service.RuntimeFlow;
import com.vx.service.IBorrowService;
import com.vx.service.IUserService;
import com.vx.vo.BorrowVO;
import com.vx.vo.UserVO;


@Controller
@RequestMapping("/borrow")
public class BorrowController {
	private String _buinessKey=null;
	private String _userCode;
	private String _candidateUsers=null;
	private String _borrowId;
	
	@Autowired
	private IBorrowService borrowService;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IRepositoryBiz repositoryBiz;
	
	
	@Autowired
	private ITaskBiz taskBiz;
	
	@Autowired
	private RuntimeFlow untimeFlow;
	
	
	@RequestMapping("list")
	public String findBorrowList(Model model,HttpServletRequest request){
		UserVO user=(UserVO) request.getSession().getAttribute("user");
		List<BorrowVO> list=borrowService.findBorrowList(user.getUserCode());
		model.addAttribute("borrowList", list);
		return "borrow/list";
	}
	
	/**
	 * 申请请假按钮事件，启动流程审批程序
	 * @param borrowId 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("startPross")
	private String startPross(String borrowId,Model model,HttpServletRequest request){
		UserVO user=(UserVO) request.getSession().getAttribute("user");
		_userCode=user.getUserCode();
		_borrowId=request.getParameter("borrowId");
		BorrowVO borrow=borrowService.findBorrowById(borrowId);
		_buinessKey=borrow.getFlowKey();
		_candidateUsers=userService.getLeader(_userCode);
		int status=untimeFlow.startPross(_borrowId,_userCode,_buinessKey,_candidateUsers);
		System.out.println(status);
		//更新请假单表的状态从0变成1（待提交-->审核中）
		borrow.setStatus(1);
		borrowService.saveBorrow(borrow);
		return "redirect:/borrow/list"; 
	}
	
	@RequestMapping("addBorrowView")
	private String addBorrowView(Model model,HttpServletRequest request){
		Map<String,String> proMap=repositoryBiz.getProcessDefinitionKeyName();
		model.addAttribute("proMap", proMap);
		return "borrow/input";
	}
	
	@RequestMapping("addBorrow")
	private String addBorrow(String title,String filesyscode,String flowKey,Model model,HttpServletRequest request){
		UserVO user=(UserVO) request.getSession().getAttribute("user");
		BorrowVO borrow = new BorrowVO();
		borrow.setTitle(title);
		borrow.setFilesyscode(filesyscode);
		borrow.setUsercode(user.getUserCode());
		borrow.setStatus(0);
		borrow.setTime(new Date(Calendar.getInstance().getTime().getTime()));
		borrow.setFlowKey(flowKey);
		try {
			borrowService.addBorrow(borrow);
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();  
		}
		return "redirect:/borrow/list"; 
	}
	
	@RequestMapping("deleteBorrow")
	private String deleteBorrow(String borrowId,Model model,HttpServletRequest request){
		borrowService.deleteBorrow(borrowId);
		return "redirect:/borrow/list"; 
	}
	
	@RequestMapping("viewTaskCurrent")
	public String viewTaskCurrent(String borrowId,Model model,HttpServletRequest request){
		//1.查询业务系统的借阅单信息
		BorrowVO borrow=borrowService.findBorrowById(borrowId);
		_borrowId=borrowId;
		_buinessKey=borrow.getFlowKey();
		//查询历史的审批信息
		List<Comment> commentList=taskBiz.getHistCommentByBorrowIdAndBuinessKey(_borrowId, _buinessKey);
		model.addAttribute("borrow",borrow);
		model.addAttribute("commentList",commentList);

		return "workflow/taskFormHis";
	}
}
